package com.lgb.controller.admin;

import com.lgb.Service.BlogService;
import com.lgb.Service.TypeService;
import com.lgb.pojo.Blog;
import com.lgb.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.lgb.vo.BlogQuery;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class BlogController {

    private static final String INPUT = "admin/blog_create";
    private static final String LIST = "admin/blog_manage";
    private static final String REDIRECT_LIST = "redirect:/admin/blog_manage";

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    /*
    list和seach_list方法的@PageableDefault的size属性值需要保持一致
     */
    @GetMapping("/blog_manage")
    //访问博客管理页面
    public String list(@PageableDefault(size = 5 , sort = {"updateTime"} ,direction = Sort.Direction.DESC)
                                   Pageable pageable, BlogQuery blog , Model model){

        //获取所有的类别，供下拉框使用
        model.addAttribute("types" , typeService.listType());
        model.addAttribute("page" , blogService.listBlog(pageable,blog));
        //return "admin/blog_manage";
        return LIST;
    }


    //搜索功能
   @PostMapping("/blog_manage/search")
    //查询-访问博客管理页面
    public String search_list(@PageableDefault(size = 5 , sort = {"updateTime"} ,direction = Sort.Direction.DESC)
                               Pageable pageable, BlogQuery blog , Model model){
        model.addAttribute("page" , blogService.listBlog(pageable,blog));
        //返回的是一个html片段
        return "admin/blog_manage :: blogList";
    }


    //跳转到新增博客页面
    @GetMapping("/blog_manage/input")
    public String input(Model model){
        //获取所有的分类
        model.addAttribute("types",typeService.listType());
        //新增时，blog的type为null，前端页面解析type.id为null，会报错
        model.addAttribute("blog" , new Blog());
        return INPUT;
    }

    //新增and更新
    @PostMapping("/blog_manage")
    public String post(Blog blog, HttpSession session , RedirectAttributes redirectAttributes){
        //把当前用户添加到blog对象中
        blog.setUser((User) session.getAttribute("user"));
        //添加获取到的Blog分类
        blog.setType(typeService.getType(blog.getType().getId()));
        //保存对象
        Blog b;
        //当blog没有id时
        if(blog.getId() == null){
            //创建新的blog对象保存
            b = blogService.saveBlog(blog);
        }else {
            //有id时，执行更新
            b = blogService.updateBlog(blog.getId(),blog);
        }
        if(b == null){
            redirectAttributes.addFlashAttribute("message","操作失败");
        }else {
            redirectAttributes.addFlashAttribute("message","操作成功");
        }
        return REDIRECT_LIST;
    }

    //跳转到修改博客页面
    @GetMapping("/blog_manage/{id}/update")
    public String update(@PathVariable Long id, Model model){
        //获取所有的分类
        model.addAttribute("types",typeService.listType());
        model.addAttribute("blog" , blogService.getBlog(id));
        return INPUT;
    }

    //删除
    @GetMapping("/blog_manage/{id}/delete")
    public String delete(@PathVariable Long id ,RedirectAttributes redirectAttributes){
        //执行删除
        blogService.deleteBlog(id);
        //信息
        redirectAttributes.addFlashAttribute("message","删除成功");
        return REDIRECT_LIST;
    }

}
